import Axios from "axios";
import errorMsg from './errorMsg'



const request = Axios.create({
    baseURL: process.env.VUE_APP_REQUEST_URL,
    timeout: 20000
})


//请求拦截器
request.interceptors.request.use(config => {

    return config
}, error => {
    // 错误抛到业务代码
    error.data = {}
    error.data.message = "服务器异常，请联系管理员！"
    return Promise.resolve(error)
})


//响应拦截器
request.interceptors.response.use(response => {

    const status = response.status
    if (status < 200 || status >= 300) {
        // 处理http错误状态码，抛到业务代码
        let msg = errorMsg(status)
        if (typeof response.data === 'string') {
            response.data = {msg}
        } else {
            response.data.message = msg
        }
    }
    return response
}, error => {
    console.log(error)
    error.data = {}
    error.data.message = "服务器异常，请联系管理员！"
    return Promise.resolve(error)
});


export default request;

